package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.esign.service.BatchSignService;
import com.winchain.chainbill.esign.vo.BatchSignVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctSignSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbDct>> {
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private BatchSignService batchSignService;
	private static final String REASON = "������������";

	public void onApplicationEvent(BatchBillEvent<String, CbDct> t) {
		String phoneCode = t.getStringValue("phoneCode");
		String location = t.getStringValue("location");
		String sealCode = t.getStringValue("sealCode");

		List<String> signBillIds = new ArrayList();
		String signBillId = null;
		List<String> records = t.getRecs();
		for (String record : records) {
			signBillId = record;
			BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(record, false);

			List<Bill<CbDct>> children = (List<Bill<CbDct>>) event.getChildren();
			if ((children != null) && (children.size() > 0)) {
				List<Bill<CbDct>> bills = children;
				for (Bill<CbDct> dctBill : bills) {
					CbDct dct = (CbDct) dctBill.getSource();
					if ((dct != null) && (dct.getDctId() != null)) {
						signBillId = dct.getBillId();
						break;
					}
				}
			}
			signBillIds.add(signBillId);
		}
		if (signBillIds.size() == 0) {
			return;
		}
		BatchSignVo batchSignVo = new BatchSignVo();
		batchSignVo.setSignBillIds(signBillIds);
		batchSignVo.setBizType("12");
		batchSignVo.setPhoneCode(phoneCode);
		batchSignVo.setLocation(location);
		batchSignVo.setReason("������������");
		batchSignVo.setSealCode(sealCode);
		this.batchSignService.batchSign(batchSignVo);
	}
}
